# -*- coding: utf-8 -*-
from selenium.webdriver.common.by import By
from web.suite.initTestSuite import init_objects
from web.keywords.baseElement import Element
from web.keywords.selenium_keywords import Page


class LoginPage(Page, Element):
    """
    登录模块，包含获取元素和测试逻辑
    """
    def __init__(self, driver, imgs, logs):
        # 初始化登录页面对象库
        super().__init__(driver, imgs, logs)
        login_objects = init_objects('login')
        self.loginpagelogs = logs.log_message('登录页面逻辑')
        self.loginpagelogs.logger.info('初始化页面元素对象：%s' % login_objects)
        self.url = login_objects.get('url')
        self.ele_username = login_objects.get('username')
        self.ele_password = login_objects.get('password')
        self.ele_login_btn = login_objects.get('login_btn')
        self.ele_per_icon = login_objects.get('per_icon')
        self.ele_logout_link = login_objects.get('logout_link')
        self.ele_err_msg = login_objects.get('err_msg')

    def login(self, username, password, casetype):
        """
        登录逻辑方法
        :param username:
        :param password: 
        :param casetype:
        :return: 
        """
        self.get_element()
        self.loginpagelogs.logger.info('调用[login]开始')
        self.open(self.url)
        self.make_maxwindow()
        print(self.ele_username)
        self.input_text(self.get_element(By.ID, self.ele_username), username)
        self.input_text(self.get_element(By.ID, self.ele_password), password)
        self.add_img()
        self.left_click(self.get_element(By.ID, self.ele_login_btn))
        if casetype == 'passed':  # 正例
            self.move_element(self.get_element(By.XPATH, self.ele_per_icon))
            self.add_img()
            login_msg = self.get_text(self.get_element(By.XPATH, self.ele_logout_link))
            self.loginpagelogs.logger.info('调用[login]结束；return:[%s]' % login_msg)
        else:  # 反例
            self.add_img()
            login_msg = self.get_text(self.get_element(By.ID, self.ele_err_msg))
            self.loginpagelogs.logger.info('调用[login]结束；return:[%s]' % login_msg)
        return login_msg
